package com.yeren.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.yeren.entity.User;
import com.yeren.service.UserService;
import com.yeren.service.impl.UserServiceImpl;
import com.yeren.utils.PageBean;
import com.yeren.utils.StringUtil;

/**
 * 账号核心处理类
 * 
 * @author liubiao
 *
 */
public class UserServlet extends HttpServlet {
	UserService us = new UserServiceImpl();
	private PrintWriter out;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String action = req.getParameter("action");
		if ("login".equals(action)) {
			login(req, resp);
		} else if ("list".equals(action)) {
			list(req, resp);
		} else if ("save".equals(action)) {
			save(req, resp);
		} else if ("delete".equals(action)) {
			delete(req, resp);
		} else if ("update".equals(action)) {
			update(req, resp);
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String imageCode = request.getParameter("imageCode");

		request.setAttribute("account", account);
		request.setAttribute("password", password);
		request.setAttribute("imageCode", imageCode);
		if (StringUtil.isEmpty(account) || StringUtil.isEmpty(password)) {
			request.setAttribute("error", "用户名或密码为空！");
			request.getRequestDispatcher("login.jsp")
					.forward(request, response);
			return;
		}
		if (StringUtil.isEmpty(imageCode)) {
			request.setAttribute("error", "验证码为空！");
			request.getRequestDispatcher("login.jsp")
					.forward(request, response);
			return;
		}
		if (!imageCode.equals(session.getAttribute("sRand"))) {
			request.setAttribute("error", "验证码错误！");
			request.getRequestDispatcher("login.jsp")
					.forward(request, response);
			return;
		}
		User user = new User();
		user.setAccount((String) request.getAttribute("account"));
		user.setPassword((String) request.getAttribute("password"));
		User currentUser = us.login(user);
		if (currentUser == null) {
			request.setAttribute("error", "用户名或密码错误！");
			request.getRequestDispatcher("login.jsp")
					.forward(request, response);
		} else {
			session.setAttribute("currentUser", currentUser);
			response.sendRedirect("crud/main.jsp");
		}

	}

	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = new User();
		user.setAccount(request.getParameter("account"));
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		PageBean pb = new PageBean(Integer.parseInt(page),
				Integer.parseInt(rows));
		List<User> list = us.findAll(user, pb);
		JSONObject obj = new JSONObject();
		obj.put("total", us.getSum());
		obj.put("rows", list);
		PrintWriter out = response.getWriter();
		out.print(obj);
		out.flush();
		out.close();
	}

	private void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = new User();
		user.setAccount(request.getParameter("account"));
		user.setAge(Integer.parseInt(request.getParameter("age")));
		user.setPassword(request.getParameter("password"));
		int save = us.save(user);
		out = response.getWriter();
		if (save == 1) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int userId = Integer.parseInt(request.getParameter("id"));
		int delete = us.delete(userId);
		out = response.getWriter();
		if (delete == 1) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = new User();
		user.setUserId(Integer.parseInt(request.getParameter("userId")));
		user.setAccount(request.getParameter("account"));
		user.setName(request.getParameter("name"));
		user.setSex(request.getParameter("sex"));
		user.setAge(Integer.parseInt(request.getParameter("age")));

		int update = us.update(user);
		out = response.getWriter();
		if (update == 1) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}

}
